<!-- @FileDescription: 公共-侧边消息提示-点击对应的弹窗组件 -->
<template>
  <div>
    <!--汇报相关-->
    <addBox ref="addBox" :daily-id="1" />
    <!--审批相关-->
    <detail-examine ref="detailExamine" />
    <!--付款记录相关-->
    <apply-for-payment ref="applyForPayment" :form-data="applyData" />
    <!--发票相关-->
    <invoice-details :form-data="invoiceData" ref="invoiceView" />
    <!--付款提醒相关-->
    <edit-contract ref="editContract" :form-data="contractData" />
    <!--客户相关-->
    <edit-customer ref="editCustomer" :form-data="customerData" />
    <!-- 低代码相关 -->
    <check-drawer v-if="checkDrawerShow" ref="checkDrawer" :keyName="keyName" :info="info"></check-drawer>
  </div>
</template>

<script>
import { toMessageDetailUrl } from '@/libs/public'
import {
  clientBillDetailApi,
  clientContractDetailApi,
  clientInvoiceDetailApi,
  chargeDetailsApi
} from '@/api/enterprise'
import { crudModuleInfoApi, crudModuleFindApi } from '@/api/develop'
export default {
  name: 'index',
  components: {
    addBox: () => import('@/views/user/daily/components/addBox'),
    detailExamine: () => import('@/views/user/examine/components/detailExamine'),
    checkDrawer: () => import('@/views/develop/module/components/checkDrawer'),
    applyForPayment: () => import('@/views/customer/list/components/applyForPayment'),
    invoiceDetails: () => import('@/components/invoice/invoiceDetails'),
    editContract: () => import('@/views/customer/contract/components/editContract'),
    editCustomer: () => import('@/views/customer/list/components/editCustomer')
  },
  props: {
    detail: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data() {
    return {
      keyName: '',
      info: {},
      checkDrawerShow: false,
      applyData: {},
      invoiceData: {},
      contractData: {},
      customerData: {},
      selectedType: ['delete', 'recall'],
      openTemplateType: [
        'daily_remind',
        'daily_show_remind',
        'daily_update_remind',
        'business_approval',
        'business_adopt_apply',
        'business_adopt_cc',
        'business_fail',
        'business_recall',
        'contract_renew',
        'contract_return_money',
        'finance_verify_success',
        'finance_verify_fail',
        'contract_invoice',
        'finance_invoice_open',
        'finance_invoice_close',
        'finance_invoice_verify_fail',
        'finance_invoice_verify_success',
        'contract_urgent_renew',
        'contract_day_remind',
        'contract_overdue_remind',
        'contract_overdue_day_remind',
        'contract_abnormal',
        'contract_soon_overdue_remind',
        'dealt_client_work',
        'dealt_money_work',
        'contract_expend',
        'system_crud_type',
        'unsettled_follow_up_remind'
      ]
    }
  },
  methods: {
    async openMessage(item, row) {
      let id = null

      if (!this.openTemplateType.includes(this.detail.template_type)) {
        // 跳转相关页面
        this.$bus.$emit('message-close-pop')
        toMessageDetailUrl(item)
        this.$emit('handleClose')
      } else {
        // 打开相关弹窗
        const type = this.detail.template_type
        if (
          [
            'contract_expend',
            'contract_renew',
            'contract_return_money',
            'finance_verify_success',
            'finance_verify_fail'
          ].includes(type)
        ) {
          id = this.detail.other.id
        } else {
          id = this.detail.link_id
        }

        if (type === 'daily_remind') {
          // 填写日报
          this.$refs.addBox.openBox(0, row.type)
        } else if (type === 'daily_show_remind' || type === 'daily_update_remind') {
          // 查看日报

          this.$refs.addBox.openBox(id, 'check', { types: row.type })
        } else if (
          [
            'business_approval',
            'business_adopt_apply',
            'business_adopt_cc',
            'business_fail',
            'business_recall'
          ].includes(type)
        ) {
          // 审批
          this.$refs.detailExamine.openBox({ id: id == 0 ? this.detail.other.id : id })
        } else if (
          [
            'contract_renew',
            'contract_return_money',
            'finance_verify_success',
            'finance_verify_fail',
            'contract_expend'
          ].includes(type)
        ) {
          // 付款审核
          this.getClientBillDetail(id)
        } else if (
          [
            'contract_invoice',
            'finance_invoice_open',
            'finance_invoice_close',
            'finance_invoice_verify_fail',
            'finance_invoice_verify_success'
          ].includes(type)
        ) {
          this.getClientInvoiceDetail(id)
        } else if (
          // 合同
          [
            'contract_urgent_renew',
            'contract_day_remind',
            'contract_overdue_remind',
            'contract_overdue_day_remind',
            'contract_abnormal',
            'contract_soon_overdue_remind',
            'dealt_money_work'
          ].includes(type)
        ) {
          this.getClientContractDetail(item, row)
        } else if (type === 'dealt_client_work' || type === 'unsettled_follow_up_remind') {
          this.getClientDataDetail(id)
        } else if (type === 'system_crud_type') {
          this.keyName = row.other.table_name_en
          this.checkDrawerShow = true
          const data = await crudModuleFindApi(this.keyName, row.other.id)
          const infoObj = await crudModuleInfoApi(this.keyName, 0)
          this.info = infoObj.data

          await this.$refs.checkDrawer.openBox(row.other, data.data, this.info, this.info.crudInfo.table_name)
        }
      }
    },
    // 付款详情
    getClientBillDetail(id) {
      clientBillDetailApi(id).then((res) => {
        this.applyData = {
          title: this.$t('customer.viewcustomer'),
          width: '500px',
          data: res.data,
          isClient: false,
          edit: true
        }
        this.$refs.applyForPayment.openBox()
      })
    },
    // 发票详情
    getClientInvoiceDetail(id) {
      clientInvoiceDetailApi(id).then((res) => {
        this.invoiceData = {
          title: '发票查看',
          width: '1000px',
          data: res.data
        }
        // 判断是否未财务发票
        if (this.detail.template_type === 'contract_invoice') {
          this.invoiceData.follType = 'fd'
        }
        this.$refs.invoiceView.openBox(res.data.link_id)
      })
    },
    // 客户合同详情
    getClientContractDetail(item, row) {
      let link_id = item.link_id ? item.link_id : row.link_id
      item.cid = item.link_id
      item.contract_name = item.linkName

      clientContractDetailApi(link_id)
        .then(async (res) => {
          const data = res.data
          item.eid = res.data.contract_customer.id

          this.contractData = {
            title: '查看合同',
            width: '1000px',
            data: row || item,
            isClient: false,
            name: row && row.client ? row.client.name : item.linkName,
            id: row && row.client ? row.client.id : item.link_id,
            edit: true
          }
          this.$refs.editContract.tabIndex = '1'
          this.$refs.editContract.tabNumber = 1
          await this.$nextTick()
          let obj = {
            cid: link_id
          }
          if (row) {
            row.id = link_id
            row.cid = row.id
          }

          this.$refs.editContract.openBox(row || obj)
        })
        .catch((error) => {
          this.$message.error(error.message)
        })
    },
    // 客户详情V1.4
    getClientDataDetail(id) {
      chargeDetailsApi(id).then(async (res) => {
        const data = res.data
        data.eid = id
        data.cid = 0
        this.customerData = {
          title: this.$t('customer.editcustomer'),
          width: '1000px',
          data: data,
          isClient: true,
          edit: true
        }

        this.$refs.editCustomer.tabIndex = '1'
        this.$refs.editCustomer.tabNumber = 1
        this.$refs.editCustomer.openBox(id)
      })
    }
  }
}
</script>

<style scoped lang="scss"></style>
